package nc.bs.impl.uapbd.incomebudget;

import com.ufida.report.anareport.FreeReportContextKey;
import com.ufida.report.anareport.base.BaseSearchTemplateCondition;
import nc.bs.dao.BaseDAO;
import nc.bs.uapbd.incomebudget.ProcessTestService;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.pub.hrpbm.util.HrpbmReportUtils;
import nc.pub.smart.context.SmartContext;
import nc.pub.smart.data.DataSet;
import nc.pub.smart.metadata.DataTypeConstant;
import nc.pub.smart.metadata.Field;
import nc.pub.smart.metadata.MetaData;
import nc.vo.pub.BusinessException;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProcessTestServiceImpl implements ProcessTestService {
    @Override
    public DataSet processData(SmartContext context) throws BusinessException {
        //获取查询条件
        BaseSearchTemplateCondition templateCondition =
                (BaseSearchTemplateCondition) context.getAttribute(FreeReportContextKey.KEY_ALLCONDITION);
        List<Map<String, Object>> executeQuery;
        HrpbmReportUtils Utils = new HrpbmReportUtils();
        //可以根据查询条件拼SQL查询
        try {

            Utils.handelContext(context);
            String isFund = (String)context.getAttribute("isfund");
            StringBuilder sql = new StringBuilder();
            if (isFund==null){
                sql.append("SELECT " +
                        "HRPBM_BUDGETMAKE.PK_GROUP AS 集团pk, " +
                        "hrpbm_budgetmake.pk_org AS 组织pk, " +
                        "HRPBM_BUDGETMAKE.NYEAR AS 年度, " +
                        "hrpbm_budgetdept.name AS 编制科室, " +
                        "hrpbm_budgetdept.pk_budgetdept AS 编制科室pk, " +
                        "CASE  " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 4 THEN 2 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 6 THEN 3 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 8 THEN 4 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 10 THEN 5 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 12 THEN 6 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 14 THEN 7 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 16 THEN 8 " +
                        "ELSE 9  " +
                        "END AS 项目级别, " +
                        "bd_inoutbusiclass.code AS 项目编码, " +
                        "bd_inoutbusiclass.name AS 项目名称, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass AS 收支项目pk, " +
                        "hrpbm_budgetmakedetail.memo AS 编制依据, " +
                        "hrpbm_budgetmakedetail.def3 AS 修改意见, " +
                        "hrpbm_budgetmakedetail.amount_firstdown AS 批复数据, " +
                        "hrpbm_budgetmakedetail.amount_adjust AS 调整金额, " +
                        "hrpbm_budgetmakedetail.amount AS 最终预算数, " +
                        "bd_defdoc.name AS 是否涉及资金 " +
                        "FROM  " +
                        "hrpbm_budgetmake " +
                        "LEFT JOIN hrpbm_budgetdept  " +
                        "ON hrpbm_budgetmake.PK_BUDGETDEPT = hrpbm_budgetdept.PK_BUDGETDEPT " +
                        "LEFT JOIN hrpbm_budgetmakedetail  " +
                        "ON hrpbm_budgetmake.PK_BUDGETMAKE = hrpbm_budgetmakedetail.PK_BUDGETMAKE " +
                        "LEFT JOIN bd_inoutbusiclass  " +
                        "ON hrpbm_budgetmakedetail.PK_INOUTCLASS = bd_inoutbusiclass.pk_inoutbusiclass " +
                        "LEFT JOIN fip_docview_b  " +
                        "ON fip_docview_b.FACTORVALUE5 = hrpbm_budgetmakedetail.PK_INOUTCLASS " +
                        "LEFT JOIN bd_defdoc  " +
                        "ON fip_docview_b.factorvalue3 = bd_defdoc.pk_defdoc " +
                        "WHERE  " +
                        "hrpbm_budgetmakedetail.PK_BUDGETTYPE = 'YS01' " +
                        "GROUP BY " +
                        "HRPBM_BUDGETMAKE.PK_GROUP, " +
                        "hrpbm_budgetmake.pk_org, " +
                        "HRPBM_BUDGETMAKE.NYEAR, " +
                        "hrpbm_budgetdept.name, " +
                        "hrpbm_budgetdept.pk_budgetdept, " +
                        "bd_inoutbusiclass.code, " +
                        "bd_inoutbusiclass.name, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass, " +
                        "hrpbm_budgetmakedetail.memo, " +
                        "hrpbm_budgetmakedetail.def3, " +
                        "hrpbm_budgetmakedetail.amount_firstdown, " +
                        "hrpbm_budgetmakedetail.amount_adjust, " +
                        "hrpbm_budgetmakedetail.amount, " +
                        "bd_defdoc.name");
            } else if (isFund.equals("01")) {
                sql.append("SELECT " +
                        "HRPBM_BUDGETMAKE.PK_GROUP AS 集团pk, " +
                        "hrpbm_budgetmake.pk_org AS 组织pk, " +
                        "HRPBM_BUDGETMAKE.NYEAR AS 年度, " +
                        "hrpbm_budgetdept.name AS 编制科室, " +
                        "hrpbm_budgetdept.pk_budgetdept AS 编制科室pk, " +
                        "CASE  " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 4 THEN 2 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 6 THEN 3 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 8 THEN 4 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 10 THEN 5 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 12 THEN 6 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 14 THEN 7 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 16 THEN 8 " +
                        "ELSE 9  " +
                        "END AS 项目级别, " +
                        "bd_inoutbusiclass.code AS 项目编码, " +
                        "bd_inoutbusiclass.name AS 项目名称, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass AS 收支项目pk, " +
                        "hrpbm_budgetmakedetail.memo AS 编制依据, " +
                        "hrpbm_budgetmakedetail.def3 AS 修改意见, " +
                        "hrpbm_budgetmakedetail.amount_firstdown AS 批复数据, " +
                        "hrpbm_budgetmakedetail.amount_adjust AS 调整金额, " +
                        "hrpbm_budgetmakedetail.amount AS 最终预算数, " +
                        "bd_defdoc.name AS 是否涉及资金 " +
                        "FROM  " +
                        "hrpbm_budgetmake " +
                        "LEFT JOIN hrpbm_budgetdept  " +
                        "ON hrpbm_budgetmake.PK_BUDGETDEPT = hrpbm_budgetdept.PK_BUDGETDEPT " +
                        "LEFT JOIN hrpbm_budgetmakedetail  " +
                        "ON hrpbm_budgetmake.PK_BUDGETMAKE = hrpbm_budgetmakedetail.PK_BUDGETMAKE " +
                        "LEFT JOIN bd_inoutbusiclass  " +
                        "ON hrpbm_budgetmakedetail.PK_INOUTCLASS = bd_inoutbusiclass.pk_inoutbusiclass " +
                        "LEFT JOIN fip_docview_b  " +
                        "ON fip_docview_b.FACTORVALUE5 = hrpbm_budgetmakedetail.PK_INOUTCLASS " +
                        "LEFT JOIN bd_defdoc  " +
                        "ON fip_docview_b.factorvalue3 = bd_defdoc.pk_defdoc " +
                        "WHERE  " +
                        "hrpbm_budgetmakedetail.PK_BUDGETTYPE = 'YS01' " +
                        "AND bd_defdoc.name='是' " +
                        "GROUP BY " +
                        "HRPBM_BUDGETMAKE.PK_GROUP, " +
                        "hrpbm_budgetmake.pk_org, " +
                        "HRPBM_BUDGETMAKE.NYEAR, " +
                        "hrpbm_budgetdept.name, " +
                        "hrpbm_budgetdept.pk_budgetdept, " +
                        "bd_inoutbusiclass.code, " +
                        "bd_inoutbusiclass.name, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass, " +
                        "hrpbm_budgetmakedetail.memo, " +
                        "hrpbm_budgetmakedetail.def3, " +
                        "hrpbm_budgetmakedetail.amount_firstdown, " +
                        "hrpbm_budgetmakedetail.amount_adjust, " +
                        "hrpbm_budgetmakedetail.amount, " +
                        "bd_defdoc.name");
            }else {
                sql.append("SELECT " +
                        "HRPBM_BUDGETMAKE.PK_GROUP AS 集团pk, " +
                        "hrpbm_budgetmake.pk_org AS 组织pk, " +
                        "HRPBM_BUDGETMAKE.NYEAR AS 年度, " +
                        "hrpbm_budgetdept.name AS 编制科室, " +
                        "hrpbm_budgetdept.pk_budgetdept AS 编制科室pk, " +
                        "CASE  " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 4 THEN 2 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 6 THEN 3 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 8 THEN 4 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 10 THEN 5 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 12 THEN 6 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 14 THEN 7 " +
                        "WHEN LENGTH(bd_inoutbusiclass.code) = 16 THEN 8 " +
                        "ELSE 9  " +
                        "END AS 项目级别, " +
                        "bd_inoutbusiclass.code AS 项目编码, " +
                        "bd_inoutbusiclass.name AS 项目名称, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass AS 收支项目pk, " +
                        "hrpbm_budgetmakedetail.memo AS 编制依据, " +
                        "hrpbm_budgetmakedetail.def3 AS 修改意见, " +
                        "hrpbm_budgetmakedetail.amount_firstdown AS 批复数据, " +
                        "hrpbm_budgetmakedetail.amount_adjust AS 调整金额, " +
                        "hrpbm_budgetmakedetail.amount AS 最终预算数, " +
                        "bd_defdoc.name AS 是否涉及资金 " +
                        "FROM  " +
                        "hrpbm_budgetmake " +
                        "LEFT JOIN hrpbm_budgetdept  " +
                        "ON hrpbm_budgetmake.PK_BUDGETDEPT = hrpbm_budgetdept.PK_BUDGETDEPT " +
                        "LEFT JOIN hrpbm_budgetmakedetail  " +
                        "ON hrpbm_budgetmake.PK_BUDGETMAKE = hrpbm_budgetmakedetail.PK_BUDGETMAKE " +
                        "LEFT JOIN bd_inoutbusiclass  " +
                        "ON hrpbm_budgetmakedetail.PK_INOUTCLASS = bd_inoutbusiclass.pk_inoutbusiclass " +
                        "LEFT JOIN fip_docview_b  " +
                        "ON fip_docview_b.FACTORVALUE5 = hrpbm_budgetmakedetail.PK_INOUTCLASS " +
                        "LEFT JOIN bd_defdoc  " +
                        "ON fip_docview_b.factorvalue3 = bd_defdoc.pk_defdoc " +
                        "WHERE  " +
                        "hrpbm_budgetmakedetail.PK_BUDGETTYPE = 'YS01' " +
                        "AND bd_defdoc.name='否' " +
                        "GROUP BY " +
                        "HRPBM_BUDGETMAKE.PK_GROUP, " +
                        "hrpbm_budgetmake.pk_org, " +
                        "HRPBM_BUDGETMAKE.NYEAR, " +
                        "hrpbm_budgetdept.name, " +
                        "hrpbm_budgetdept.pk_budgetdept, " +
                        "bd_inoutbusiclass.code, " +
                        "bd_inoutbusiclass.name, " +
                        "bd_inoutbusiclass.pk_inoutbusiclass, " +
                        "hrpbm_budgetmakedetail.memo, " +
                        "hrpbm_budgetmakedetail.def3, " +
                        "hrpbm_budgetmakedetail.amount_firstdown, " +
                        "hrpbm_budgetmakedetail.amount_adjust, " +
                        "hrpbm_budgetmakedetail.amount, " +
                        "bd_defdoc.name");
            }
            // list是行的list，每个列都会按 列名与值 的映射关系放到map里，直接用列名get就行
            executeQuery = (List<Map<String, Object>>) this.getBaseDAO().executeQuery(sql.toString(), new MapListProcessor());
        }
        catch (Exception e) {
            System.out.println("===============查询异常==================");
            throw new BusinessException(e);
        }

        // 构造结果集
        DataSet dataSet = new DataSet();
        // 结果集分为两部分
        // 第一部分是数据部分，要求是object[][]的二维数组，就是结果集的一种体现
        Object[][] datas;
        List<Object[]> dataList = new ArrayList<>();

        // datas可以根据上述executeQuery组装
        for (Map<String, Object> rowMap : executeQuery) {
            //每一行数据
            List<Object> row = new ArrayList<>();
            //按照查询结果的顺序添加列数据
            row.add(rowMap.get("集团pk"));
            row.add(rowMap.get("组织pk"));
            row.add(rowMap.get("年度"));
            row.add(rowMap.get("编制科室"));
            row.add(rowMap.get("编制科室pk"));
            row.add(rowMap.get("项目级别"));
            row.add(rowMap.get("项目编码"));
            row.add(rowMap.get("项目名称"));
            row.add(rowMap.get("收支项目pk"));
            row.add(rowMap.get("编制依据"));
            row.add(rowMap.get("修改意见"));
            row.add(rowMap.get("批复数据"));
            row.add(rowMap.get("调整金额"));
            row.add(rowMap.get("最终预算数"));
            row.add(rowMap.get("是否涉及资金"));
            //将这一行数据加入最终结果里
            dataList.add(row.toArray(new Object[0]));
        }

        //转换为二维数组
        datas = dataList.toArray(new Object[0][]);
        // 构造好了之后set进来就可以
        dataSet.setDatas(datas);

        // 第二部分是元数据部分，元数据是指每列的字段信息，例如列名，数据类型，精度等信息，
        // 构造时需要和datas的列的顺序一一对应才可以
        MetaData metaData = new MetaData();

        // 添加集团pk字段
        Field groupField = new Field();
        groupField.setCaption("集团pk");
        groupField.setDbColumnType(Types.VARCHAR);
        groupField.setDataType(DataTypeConstant.STRING);
        groupField.setFldname("pk_group");
        groupField.setPrecision(100);
        metaData.addField(groupField);

        // 添加项目编码字段
        Field orgField = new Field();
        orgField.setCaption("组织pk");
        orgField.setDbColumnType(Types.VARCHAR);
        orgField.setDataType(DataTypeConstant.STRING);
        orgField.setFldname("pk_org");
        orgField.setPrecision(100);
        metaData.addField(orgField);

        // 添加年度字段
        Field yearField = new Field();
        yearField.setCaption("年度");                        // 字段名
        yearField.setDbColumnType(Types.VARCHAR);           // jdbc类型
        yearField.setDataType(DataTypeConstant.STRING);     // java类型
        yearField.setFldname("nyear");                      // 字段编码
        yearField.setPrecision(4);                          // ⻓度
        metaData.addField(yearField);

        // 添加编制科室字段
        Field deptField = new Field();
        deptField.setCaption("编制科室");
        deptField.setDbColumnType(Types.VARCHAR);
        deptField.setDataType(DataTypeConstant.STRING);
        deptField.setFldname("bzks");
        deptField.setPrecision(100);
        metaData.addField(deptField);

        // 添加编制科室pk字段
        Field deptPkField = new Field();
        deptPkField.setCaption("编制科室pk");
        deptPkField.setDbColumnType(Types.VARCHAR);
        deptPkField.setDataType(DataTypeConstant.STRING);
        deptPkField.setFldname("pk_dept");
        deptPkField.setPrecision(100);
        metaData.addField(deptPkField);

        // 添加项目级别字段
        Field levelField = new Field();
        levelField.setCaption("项目级别");
        levelField.setDbColumnType(Types.INTEGER);
        levelField.setDataType(DataTypeConstant.STRING);
        levelField.setFldname("xmjb");
        levelField.setScale(0);
        metaData.addField(levelField);

        // 添加项目编码字段
        Field codeField = new Field();
        codeField.setCaption("项目编码");
        codeField.setDbColumnType(Types.VARCHAR);
        codeField.setDataType(DataTypeConstant.STRING);
        codeField.setFldname("xmbm");
        codeField.setPrecision(50);
        metaData.addField(codeField);

        // 添加项目名称字段
        Field nameField = new Field();
        nameField.setCaption("项目名称");
        nameField.setDbColumnType(Types.VARCHAR);
        nameField.setDataType(DataTypeConstant.STRING);
        nameField.setFldname("xmmc");
        nameField.setPrecision(200);
        metaData.addField(nameField);

        // 添加收支项目pk字段
        Field incomeField = new Field();
        incomeField.setCaption("收支项目pk");
        incomeField.setDbColumnType(Types.VARCHAR);
        incomeField.setDataType(DataTypeConstant.STRING);
        incomeField.setFldname("pk_income");
        incomeField.setPrecision(100);
        metaData.addField(incomeField);

        // 添加编制依据字段
        Field memoField = new Field();
        memoField.setCaption("编制依据");
        memoField.setDbColumnType(Types.VARCHAR);
        memoField.setDataType(DataTypeConstant.STRING);
        memoField.setFldname("bzyj");
        memoField.setPrecision(5000);
        metaData.addField(memoField);

        // 添加修改意见字段
        Field def3Field = new Field();
        def3Field.setCaption("修改意见");
        def3Field.setDbColumnType(Types.VARCHAR);
        def3Field.setDataType(DataTypeConstant.STRING);
        def3Field.setFldname("xgyj");
        def3Field.setPrecision(5000);
        metaData.addField(def3Field);

        // 添加批复数据字段
        Field firstDownField = new Field();
        firstDownField.setCaption("批复数据");
        firstDownField.setDbColumnType(Types.DECIMAL);
        firstDownField.setDataType(DataTypeConstant.DOUBLE);
        firstDownField.setFldname("pfsj");
        firstDownField.setPrecision(20);
        firstDownField.setScale(2);
        metaData.addField(firstDownField);

        // 添加调整金额字段
        Field adjustField = new Field();
        adjustField.setCaption("调整金额");
        adjustField.setDbColumnType(Types.DECIMAL);
        adjustField.setDataType(DataTypeConstant.DOUBLE);
        adjustField.setFldname("tzje");
        adjustField.setPrecision(20);
        adjustField.setScale(2);
        metaData.addField(adjustField);

        // 添加最终预算数字段
        Field amountField = new Field();
        amountField.setCaption("最终预算数");
        amountField.setDbColumnType(Types.DECIMAL);
        amountField.setDataType(DataTypeConstant.DOUBLE);
        amountField.setFldname("zzyss");
        amountField.setPrecision(20);
        amountField.setScale(2);
        metaData.addField(amountField);

        // 添加是否涉及资金字段
        Field isInvolvedField = new Field();
        isInvolvedField.setCaption("是否涉及资金");
        isInvolvedField.setDbColumnType(Types.VARCHAR);
        isInvolvedField.setDataType(DataTypeConstant.STRING);
        isInvolvedField.setFldname("is_fund");
        isInvolvedField.setPrecision(100);
        metaData.addField(isInvolvedField);

        // 构造好了之后set进去
        dataSet.setMetaData(metaData);
        return dataSet;
    }

    private BaseDAO baseDAO;

    public BaseDAO getBaseDAO() {
        return baseDAO == null ? new BaseDAO() : baseDAO;
    }
}
